package com.cennavi.minenavidemo.activity.geocode;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.presenter.MapPresenter;
import com.cennavi.minenavidemo.presenter.PoiPresenter;
import com.cennavi.minenavidemo.widget.MyMapView;
import com.minedata.minenavi.map.CompassView;
import com.minedata.minenavi.map.MapView;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.map.ScaleView;
import com.minedata.minenavi.map.TrafficEventInfo;
import com.minedata.minenavi.map.ZoomView;
import com.minedata.minenavi.mapdal.LatLng;
import com.minedata.minenavi.mapdal.NcPoiFavoriteItem;
import com.minedata.minenavi.mapdal.PoiItem;
import com.minedata.minenavi.mapdal.Tools;
import com.minedata.minenavi.poiquery.DistrictItem;
import com.minedata.minenavi.poiquery.GeocodeSearch;
import com.minedata.minenavi.poiquery.RegeocodeAddress;
import com.minedata.minenavi.poiquery.Tip;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 逆地理编码搜索
 */
public class ChooseMapPointActivity extends AppCompatActivity implements PoiPresenter.OnPoiSearchListener {

    private MineMap mMineMap;
    private GeocodeSearch geocoderSearch;
    private boolean isDrawEnd = false;
    private List<NcPoiFavoriteItem> items = new ArrayList<>();
    private List<PoiItem> collectedPoiItems = new ArrayList<>();
    private PoiItem mPoiItem;
    private boolean isFavorite = false;
    private boolean isCollected = false;
    private int index;
    private MapPresenter mMapPresenter;
    private PoiPresenter mPoiPresenter;

    private Unbinder mUnbinder;
    @BindView(R.id.mapView)
    MyMapView mMapView;
    @BindView(R.id.compassView)
    CompassView mCompassView;
    @BindView(R.id.zoomView)
    ZoomView mZoomView;
    @BindView(R.id.scaleView)
    ScaleView mScaleView;
    @BindView(R.id.tv_name)
    TextView mNameTextView;
    @BindView(R.id.tv_address)
    TextView mAddressTextView;
    @BindView(R.id.btn_confirm)
    Button mConfirmButton;
    @BindView(R.id.ll_collect)
    LinearLayout ll_collect;
    @BindView(R.id.iv_collect)
    ImageView iv_collect;
    @BindView(R.id.tv_collect)
    TextView tv_collect;

    Handler handler = new Handler();
    Runnable regeocodeRunnable = new Runnable() {
        @Override
        public void run() {
            Point point = mMineMap.getWorldCenter();
            mPoiPresenter.reGeocode(Tools.pointToLatLonPoint(point));
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_map_point);
        mUnbinder = ButterKnife.bind(this);
        initView();
        init();
    }

    public void initView() {
        mCompassView.setMapView(mMapView);

        mZoomView.setMapView(mMapView);
        String[] zoomOutFileNames = new String[]{
                "navicore/res/zoomButton/zoom_out_normal.png",
                "navicore/res/zoomButton/zoom_out_disable.png",
                "navicore/res/zoomButton/zoom_out_pressed.png"};
        String[] zoomInFileNames = new String[]{
                "navicore/res/zoomButton/zoom_in_normal.png",
                "navicore/res/zoomButton/zoom_in_disable.png",
                "navicore/res/zoomButton/zoom_in_pressed.png"};
        int[] types = new int[]{ZoomView.Type.normal, ZoomView.Type.disabled,
                ZoomView.Type.pressed};
        mZoomView.setZoomOutBackground(zoomOutFileNames, types);
        mZoomView.setZoomInBackground(zoomInFileNames, types);

        mScaleView.setMapView(mMapView);
        mScaleView.setDrawPos(ScaleView.Pos.leftBottom);
    }

    public void init() {
        mMapPresenter = MapPresenter.getInstance();
        mMapPresenter.init(ChooseMapPointActivity.this, mMapView);
        mMapPresenter.setOnMapViewListener(new MapPresenter.OnMapViewListener() {
            @Override
            public void onMapReady() {
                mMineMap = mMapPresenter.getMineMap();
                //设置地图中心点，刷新
                Point point = Tools.latLngToPoint(new LatLng(41.834347, 123.548782));
                mMineMap.setPointToCenter(point.x, point.y);
                mMineMap.setZoomLevel(11);
                mMapPresenter.addFavoritePoiLayer();

                //先设置mapview监听事件
                mMapView.addListener(new MyMapView.Listener() {
                    @Override
                    public void onCameraChanged(int changeType) {
                        //map为空判断
                        if (null != mMineMap) {
                            mCompassView.updateHeading(mMineMap.getHeading());
                            mScaleView.updateScale(mMineMap.getZoomLevel());
                            handler.removeCallbacks(regeocodeRunnable);

                            if (isDrawEnd) {
                                handler.postDelayed(regeocodeRunnable, 100);
                            } else {
                                handler.postDelayed(regeocodeRunnable, 1000);
                            }
                        }
                    }

                    @Override
                    public void onTrafficEventIconClicked(int type, TrafficEventInfo trafficEventInfo) {

                    }

                    @Override
                    public void onScrollStarted() {

                    }

                    @Override
                    public void onScrollFinished() {

                    }
                });

                mMapView.addOnDrawListener(new MapView.OnDrawListener() {
                    @Override
                    public void onDrawBegin() {
                        isDrawEnd = false;
                    }

                    @Override
                    public void onDrawEnd() {
                        isDrawEnd = true;
                    }
                });
            }

            @Override
            public void onswitchLocation(int imageLevel) {

            }
        });

        mPoiPresenter = PoiPresenter.getInstance();
        mPoiPresenter.init(ChooseMapPointActivity.this);
        mPoiPresenter.setOnPoiSearchListener(this);
        mPoiPresenter.initRegeocode();
    }

    @OnClick(R.id.btn_confirm)
    public void confirm() {
        Intent intent = new Intent();
        Point point = mMineMap.getWorldCenter();
        intent.putExtra("name", mNameTextView.getText().toString());
        intent.putExtra("poiX", point.x);
        intent.putExtra("poiY", point.y);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @OnClick(R.id.ll_collect)
    public void collect() {
        if (isFavorite) {
            iv_collect.setImageResource(R.drawable.poi_info_view_uncollect);
            tv_collect.setText("收藏");
            isFavorite = false;

            if (isCollected) {
                items.remove(index);
                collectedPoiItems.remove(index);
                isCollected = false;
            } else {
                items.remove(items.size() - 1);
                collectedPoiItems.remove(collectedPoiItems.size() - 1);
            }

            mMapPresenter.showFavoritePoiLayer(items);
        } else {
            iv_collect.setImageResource(R.drawable.poi_info_view_collect);
            tv_collect.setText("已收藏");
            isFavorite = true;
            NcPoiFavoriteItem ncPoiFavoriteItem = new NcPoiFavoriteItem(mPoiItem);
            items.add(ncPoiFavoriteItem);
            mMapPresenter.showFavoritePoiLayer(items);
            collectedPoiItems.add(mPoiItem);
        }
    }

    @Override
    protected void onPause() {
        if (mMapView != null) {
            mMapView.onPause();
        }

        super.onPause();
    }

    @Override
    protected void onResume() {
        if (mMapView != null) {
            mMapView.onResume();
        }

        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if (mMapView != null) {
            mMapView.onDestroy();
        }

        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    public void onSearchComplete(ArrayList<PoiItem> poiItems, boolean isSuccess) {

    }

    @Override
    public void onIdSearch(PoiItem poiItem, boolean isSuccess) {

    }

    @Override
    public void onRegeocodeSearched(RegeocodeAddress regeocodeAddress) {

        List<PoiItem> poiItems = regeocodeAddress.getPois();

        if (poiItems != null && poiItems.size() > 0) {
            mPoiItem = poiItems.get(0);

            mNameTextView.setText(mPoiItem.title);
            mAddressTextView.setText(mPoiItem.snippet);

            isCollected = false;

            for (int i = 0; i < collectedPoiItems.size(); i++) {
                PoiItem collectedPoiItem = collectedPoiItems.get(i);
                if (collectedPoiItem.weakEquals(mPoiItem)) {
                    isCollected = true;
                    index = i;
                    break;
                }
            }

            if (!isCollected) {
                iv_collect.setImageResource(R.drawable.poi_info_view_uncollect);
                tv_collect.setText("收藏");
                isFavorite = false;
            } else {
                iv_collect.setImageResource(R.drawable.poi_info_view_collect);
                tv_collect.setText("已收藏");
                isFavorite = true;
            }
        }
    }

    @Override
    public void onDistrictSearch(DistrictItem districtItem) {

    }

    @Override
    public void onGetInputtips(List<Tip> lists) {

    }
}
